 package com.cmfi.controller;

import java.net.URISyntaxException;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cmfi.domain.AssembleQuality;
import com.cmfi.service.inter.ForkliftQualityService;
import com.cmfi.utils.Code;
import com.cmfi.utils.Result;

/**
 * REST controller for managing {@link com.AssembleQuality.forklift.domain.ForkliftQuality}.
 */
@RestController
@RequestMapping("/api")
public class ForkliftQualityController {

    private final Logger log = LoggerFactory.getLogger(ForkliftQualityController.class);

    @Autowired
    private ForkliftQualityService forkliftQualityService;

    /**
     * {@code POST  /forklift-qualities} : Create a new forkliftQuality.
     *
     * @param forkliftQuality the forkliftQuality to create.
     * @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new forkliftQuality, or with status {@code 400 (Bad Request)} if the forkliftQuality has already an ID.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
    @PostMapping("/forklift-qualities")
    @PreAuthorize("hasAuthority('qualityRecord')")
	public Result createForkliftQuality(@RequestBody AssembleQuality forkliftQuality) {
		log.info("[ForkliftQualityController][createForkliftQuality] forkliftQuality: {}", forkliftQuality);
		if (forkliftQuality.getId() != null) {
			return new Result(Code.SAVE_ERR, "id不为空");
		}
		try {
			AssembleQuality quality = forkliftQualityService.save(forkliftQuality);
			return new Result(Code.SAVE_OK, quality);
		} catch (Exception e) {
			log.info("[ForkliftQualityController][createForkliftQuality] e,", e);
			return new Result(Code.SAVE_ERR, e.getMessage());
		}
    }

    /**
     * {@code PUT  /forklift-qualities/:id} : Updates an existing forkliftQuality.
     *
     * @param id the id of the forkliftQuality to save.
     * @param forkliftQuality the forkliftQuality to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated forkliftQuality,
     * or with status {@code 400 (Bad Request)} if the forkliftQuality is not valid,
     * or with status {@code 500 (Internal Server Error)} if the forkliftQuality couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
//	@PutMapping("/forklift-qualities/{id}")
//	@PreAuthorize("hasAuthority('qualityRecord')")
//	public Result updateForkliftQuality(@PathVariable(value = "id", required = false) final Long id,
//			@RequestBody ForkliftQuality forkliftQuality) throws URISyntaxException {
//		log.info("[ForkliftQualityController][updateForkliftQuality]id: {}, forkliftQuality:{}", id, forkliftQuality);
//		if (id == null) {
//			return new Result(Code.SAVE_ERR, "id是空的");
//		}
//		try {
//			ForkliftQuality quality = forkliftQualityService.update(id, forkliftQuality);
//			return new Result(Code.SAVE_OK, quality);
//		} catch (Exception e) {
//			log.info("[ForkliftQualityController][updateForkliftQuality] e,", e);
//			return new Result(Code.SAVE_ERR, e.getMessage());
//		}
//	}

    /**
     * {@code PATCH  /forklift-qualities/:id} : Partial updates given fields of an existing forkliftQuality, field will ignore if it is null
     *
     * @param id the id of the forkliftQuality to save.
     * @param forkliftQuality the forkliftQuality to update.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated forkliftQuality,
     * or with status {@code 400 (Bad Request)} if the forkliftQuality is not valid,
     * or with status {@code 404 (Not Found)} if the forkliftQuality is not found,
     * or with status {@code 500 (Internal Server Error)} if the forkliftQuality couldn't be updated.
     * @throws URISyntaxException if the Location URI syntax is incorrect.
     */
//	@PatchMapping("/forklift-qualities/{id}")
//	@PreAuthorize("hasAuthority('qualityRecord')")
//	public Result partialUpdateForkliftQuality(@PathVariable(value = "id", required = false) final Long id,
//			@RequestBody ForkliftQuality forkliftQuality) throws URISyntaxException {
//		log.info("[ForkliftQualityController][partialUpdateForkliftQuality] : {}, {}", id, forkliftQuality);
//		if (id == null) {
//			return new Result(Code.UPDATE_ERR, "id是空的");
//		}
//		try {
//			ForkliftQuality forkQuality = forkliftQualityService.partialUpdate(id, forkliftQuality);
//			return new Result(Code.UPDATE_OK, forkQuality);
//		} catch (Exception e) {
//			log.error("[ForkliftQualityController][partialUpdateForkliftQuality] e,", e);
//			return new Result(Code.UPDATE_ERR, e.getMessage());
//		}
//	}

    /**
     * {@code GET  /forklift-qualities} : get all the forkliftQualities.
     *
     * @param pageable the pagination information.
     * @param request a {@link ServerHttpRequest} request.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of forkliftQualities in body.
     */
	@GetMapping("/forklift-qualities")
	@PreAuthorize("hasAuthority('qualityRecord')")
	public Result getAllForkliftQualities(@RequestParam Map<String, Object> params) {
		log.info("[ForkliftQualityController][getAllForkliftQualities] params:{}", params);
		long pageNum = 0;
		long pageSize = 0;
		long total = 0;
		try {
			if (params.containsKey("pageNum")) {
				pageNum = Long.parseLong(params.get("pageNum").toString());
			}
			if (params.containsKey("pageSize")) {
				pageSize = Long.parseLong(params.get("pageSize").toString());
			}

			List<AssembleQuality> qualityList = forkliftQualityService.findByPage(pageNum, pageSize);
		total = qualityList.size();
			return new Result(Code.GET_OK, qualityList, total);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getAllForkliftQualities] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
	}

    /**
     * {@code GET  /forklift-qualities/:id} : get the "id" forkliftQuality.
     *
     * @param id the id of the forkliftQuality to retrieve.
     * @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the forkliftQuality, or with status {@code 404 (Not Found)}.
     */
	@GetMapping("/forklift-qualities/{id}")
	@PreAuthorize("hasAuthority('qualityRecord')")
	public Result getForkliftQuality(@PathVariable Long id) {
		log.info("[ForkliftQualityController][getForkliftQuality] id:{}", id);
		try {
			AssembleQuality forkliftQuality = forkliftQualityService.findOne(id);
			return new Result(Code.GET_OK, forkliftQuality);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getForkliftQuality] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
	}

    /**
     * {@code DELETE  /forklift-qualities/:id} : delete the "id" forkliftQuality.
     *
     * @param id the id of the forkliftQuality to delete.
     * @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
     */
	@DeleteMapping("/forklift-qualities/{id}")
	@PreAuthorize("hasAuthority('qualityRecord')")
	public Result deleteForkliftQuality(@PathVariable Long id) {
		log.info("[ForkliftQualityController][getForkliftQuality] id:{}", id);
		try {
			forkliftQualityService.delete(id);
			return new Result(Code.DELETE_OK, id);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getForkliftQuality] e,", e);
			return new Result(Code.DELETE_ERR, e.getMessage());
		}
	}

    @PostMapping("/forklift-qualities/submit")
    @PreAuthorize("hasAuthority('qualityRecord')")
	public Result submitForkListPart(@RequestBody List<AssembleQuality> forkliftQualities) {
		log.info("[ForkliftQualityController][submitForkListPart] forkliftQualities:{}", forkliftQualities);
		if (forkliftQualities.isEmpty()) {
			return new Result(Code.SAVE_ERR, "质量信息为空");
		}
		try {
			List<AssembleQuality> qualityList = forkliftQualityService.submit(forkliftQualities);
			return new Result(Code.SAVE_OK, qualityList);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][submitForkListPart] e,", e);
			return new Result(Code.SAVE_ERR, e.getMessage());
		}
	}

    @GetMapping("/forklift-qualities/getQualityByVinCode")
    @PreAuthorize("hasAuthority('qualityRecord')")
    public Result getQualityByVinCode(@RequestParam String vinCode) {
        log.info("[ForkliftQualityController][getQualityByVinCode] vinCode: {}", vinCode);
        try {
        	List<AssembleQuality> qualityList = forkliftQualityService.getQualityByVinCode(vinCode);
        	long total = qualityList.size();
			return new Result(Code.GET_OK, qualityList, total);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getQualityByVinCode] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
    }

    @GetMapping("/forklift-qualities/getQualityByPosition")
    @PreAuthorize("hasAuthority('qualityRecord')")
    public Result getQualityByPosition(@RequestParam String position) {
        log.info("[ForkliftQualityController][getQualityByPosition]  position:{}", position);
        try {
        	 List<AssembleQuality> positionList = forkliftQualityService.getQualityByPosition(position);
        	 long total = positionList.size();
			return new Result(Code.GET_OK, positionList, total);
		} catch (Exception e) {
			log.error("[ForkliftQualityController][getQualityByPosition] e,", e);
			return new Result(Code.GET_ERR, e.getMessage());
		}
    }
}
